package com.example.oasystem.controller.task;




import com.example.oasystem.dao.task.AoaTaskListDao;
import com.example.oasystem.dao.task.AoaTaskListDaoOfSun;
import com.example.oasystem.entity.dept.AoaDept;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.position.AoaPosition;
import com.example.oasystem.entity.process.AoaUserProcessOfSun;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.task.*;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaTaskListServiceOfSun;
import com.example.oasystem.service.AoaUserProcessServiceOfSun;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TaskManageController {
    @Autowired
    private AoaTaskListServiceOfSun aoaTaskListServiceOfSun;

    @Autowired
    private AoaUserProcessServiceOfSun aoaUserProcessServiceOfSun;

    @Autowired
    private AoaTaskListDaoOfSun aoaTaskListDaoOfSun;
    @Autowired
    AoaTaskListDao taskListDao;

    @RequestMapping("/taskmanage")
    public String selectUserAllTask(Model model,
                                    HttpServletRequest request,
                                    @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                                    @RequestParam(value = "baseKey", defaultValue = "") String baseKey){
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaTaskListOfSun> prolist = aoaTaskListServiceOfSun.selectselectUserAllTask(pageNum,pageSize,baseKey,user.getUserId(),0);
        Page page=new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()),prolist.getPages(),prolist.getPageNum(),prolist.isIsFirstPage(),prolist.isIsLastPage());
        model.addAttribute("tasklist",prolist.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","taskpaging");
        return "task/taskmanage";
    }

    @RequestMapping("/taskpaging")
    public String taskpaging(Model model,
                             HttpServletRequest request,
                             @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey){

        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaTaskListOfSun> prolist = aoaTaskListServiceOfSun.selectselectUserAllTask(pageNum,pageSize,baseKey,user.getUserId(),0);
        Page page=new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()),prolist.getPages(),prolist.getPageNum(),prolist.isIsFirstPage(),prolist.isIsLastPage());
        model.addAttribute("tasklist",prolist.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","taskpaging");
        return "task/taskmanagetable";
    }


    @RequestMapping("/addtask")
    public String addTask( Model model,HttpServletRequest request,
                           @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                           @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ){
        List<AoaTypeList> aoaTypeLists = aoaTaskListServiceOfSun.typeList();

        List<AoaStatusList> aoaStatusLists = aoaTaskListServiceOfSun.statusList();

        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaUserProcessOfSun> prolist = aoaUserProcessServiceOfSun.selectUserSubordinate(pageNum,pageSize,userId,baseKey);
        List<AoaPosition> positions = aoaUserProcessServiceOfSun.selectPosition();
        List<AoaDept> depts = aoaUserProcessServiceOfSun.selectDept();
        Page page=new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()),prolist.getPages(),prolist.getPageNum(),prolist.isIsFirstPage(),prolist.isIsLastPage());
        model.addAttribute("emplist",prolist.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","addTaskpaging");
        model.addAttribute("typelist",aoaTypeLists);
        model.addAttribute("statuslist",aoaStatusLists);
        model.addAttribute("poslist",positions);
        model.addAttribute("deptlist",depts);
        return "task/addtask";
    }

    @RequestMapping("/addTaskpaging")
    public String addTaskpaging( Model model,HttpServletRequest request,
                                 @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                                 @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ){
        List<AoaTypeList> aoaTypeLists = aoaTaskListServiceOfSun.typeList();

        List<AoaStatusList> aoaStatusLists = aoaTaskListServiceOfSun.statusList();

        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaUserProcessOfSun> prolist = aoaUserProcessServiceOfSun.selectUserSubordinate(pageNum,pageSize,userId,baseKey);
        List<AoaPosition> positions = aoaUserProcessServiceOfSun.selectPosition();
        List<AoaDept> depts = aoaUserProcessServiceOfSun.selectDept();
        Page page=new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()),prolist.getPages(),prolist.getPageNum(),prolist.isIsFirstPage(),prolist.isIsLastPage());
        model.addAttribute("emplist",prolist.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","addTaskpaging");
        model.addAttribute("typelist",aoaTypeLists);
        model.addAttribute("statuslist",aoaStatusLists);
        model.addAttribute("poslist",positions);
        model.addAttribute("deptlist",depts);
        return "common/reciversOfSun";
    }



    @RequestMapping("/ck_addtask")
    public String insertTask(AoaTaskListOfSun task,HttpServletRequest request,@RequestParam(value = "top",required = false,defaultValue = "")String top,@RequestParam(value = "cancel",required = false,defaultValue = "")String cancel){
        if (top.equals("on")){
            task.setIsTop(1);
        }else {
            task.setIsTop(0);
        }
        if (cancel.equals("on")){
            task.setIsCancel(1);
        }else {
            task.setIsCancel(0);
        }
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        task.setPublishTime(format.format(date));
        task.setModifyTime(format.format(date));
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        task.setTaskPushUserId(user.getUserId());
        System.out.println("task kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk = " + task);
        String[] split = task.getReciverlist().split(";");
        List<AoaUser> users=aoaTaskListServiceOfSun.selectUserIdAll(split);
        aoaTaskListServiceOfSun.insertTask(task,users);
        return "forward:/taskmanage";
    }

    @RequestMapping("/edittasks")
    public String updateTask(Model model,HttpServletRequest request,
                             @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                             @RequestParam("id")long id
    ) {

        System.out.println("id dddddddddddddddddddddddddddddddddddddddddddddd  = " + id);
        List<AoaTypeList> aoaTypeLists = aoaTaskListServiceOfSun.typeList();

        List<AoaStatusList> aoaStatusLists = aoaTaskListServiceOfSun.statusList();

        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        if (baseKey.equals(",")) {
            baseKey = "";
        }
        List<AoaTaskListOfSun> taskListOfSuns = aoaTaskListDaoOfSun.selectselectUserAllTask("", 0, id);
        PageInfo<AoaUserProcessOfSun> prolist = aoaUserProcessServiceOfSun.selectUserSubordinate(pageNum, pageSize,userId, baseKey);
        List<AoaPosition> positions = aoaUserProcessServiceOfSun.selectPosition();
        List<AoaDept> depts = aoaUserProcessServiceOfSun.selectDept();
        Page page = new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()), prolist.getPages(), prolist.getPageNum(), prolist.isIsFirstPage(), prolist.isIsLastPage());
        model.addAttribute("emplist", prolist.getList());
        model.addAttribute("page", page);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("url", "addTaskpaging");
        model.addAttribute("typelist", aoaTypeLists);
        model.addAttribute("statuslist", aoaStatusLists);
        model.addAttribute("poslist", positions);
        model.addAttribute("deptlist", depts);
        model.addAttribute("task",taskListOfSuns.get(0));
        return "task/edittask";
    }


    @RequestMapping("/updateTask")
    public String updateTaskUser(AoaTaskListOfSun task,HttpServletRequest request,
                                 @RequestParam(value = "top",required = false,defaultValue = "")String top,@RequestParam(value = "cancel",required = false,defaultValue = "")String cancel){
        if (top.equals("on")){
            task.setIsTop(1);
        }else {
            task.setIsTop(0);
        }
        if (cancel.equals("on")){
            task.setIsCancel(1);
        }else {
            task.setIsCancel(0);
        }
        System.out.println("task222222222222222222222222222222222 = " + task);
        List<AoaTaskListOfSun> aoaTaskListOfSuns = aoaTaskListDaoOfSun.selectselectUserAllTask("", 0, task.getTaskId());
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        task.setPublishTime(format.format(date));
        task.setModifyTime(format.format(date));
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        task.setTaskPushUserId(user.getUserId());
        if (task.getStatusId()==null){
            task.setStatusId(aoaTaskListOfSuns.get(0).getStatusId());
        }
//        String[] split = task.getReciverlist().split(";");
        aoaTaskListServiceOfSun.updateTaskUser(task);
//        String[] split1 = aoaTaskListOfSuns.get(0).getReciverlist().split(";");
//
//        if (split.length != split1.length){
//            String s="";
//            for (String s1 : split) {
//                for (String s2 : split1) {
//                   s+= s1.equals(s2)?"":s1+";";
//                }
//            }
//           if (!s.equals("")){
//               String[] unames = s.split(";");
//               List<AoaUser> aoaUsers = aoaTaskListServiceOfSun.selectUserIdAll(unames);
//               aoaTaskListServiceOfSun.updateTaskUserStatus(task.getTaskId(),aoaUsers);
//           }
//        }


        return "forward:/taskmanage";
    }
    @RequestMapping("/seetasks")
    public String seetasks(@RequestParam(value = "id",defaultValue = "0") int id,
                           @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                           @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                           Model model, HttpSession session){
        AoaUser user = (AoaUser)session.getAttribute("user");
        List<AoaTaskListOfSun> taskListOfSuns = aoaTaskListDaoOfSun.selectselectUserAllTask("", user.getUserId(), id);
        System.out.println("taskListOfSuns 11111111111111111111111111111111111111111111= " + taskListOfSuns);
        List<AoaTaskLoggerOfSun> aoaTaskLoggerOfSuns = aoaTaskListServiceOfSun.selectLogger(id);
        List<AoaStatusList> aoaStatusLists = aoaTaskListServiceOfSun.statusList();
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("statuslist",aoaStatusLists);
        model.addAttribute("loggerlist",aoaTaskLoggerOfSuns);
        model.addAttribute("task",taskListOfSuns.get(0));
        model.addAttribute("baseKey",baseKey);
        return "task/seetask";
    }
    @RequestMapping("/tasklogger")
    public String tasklogger(HttpServletRequest request,@RequestParam("taskId")long taskId,@RequestParam("loggerStatusid")int loggerStatusid ,@RequestParam(value = "loggerTicking",required = false,defaultValue = "")String loggerTicking){
        AoaUser user =(AoaUser) request.getSession().getAttribute("user");
        aoaTaskListServiceOfSun.updateTaskUserStatusId(taskId,loggerStatusid);
        aoaTaskListDaoOfSun.updateTaskUserStatusId2(taskId,loggerStatusid);
        AoaTaskLoggerOfSun loggerOfSun = new AoaTaskLoggerOfSun();
        String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        loggerOfSun.setCreateTime(s);
        loggerOfSun.setLoggerStatusid(loggerStatusid);
        loggerOfSun.setLoggerTicking(loggerTicking);
        loggerOfSun.setTaskId(taskId);
        loggerOfSun.setUsername(user.getUserName());
        aoaTaskListServiceOfSun.addlogger(loggerOfSun);
        return "forward:/taskmanage";
    }
    @RequestMapping("shanchu")
    public String deleteTask(@RequestParam(value = "id", defaultValue = "-1") int taskId, @RequestParam(value = "pageNum",defaultValue = "1")int pageNum, HttpSession session, Model model) {
        taskListDao.deleteTaskById(taskId);
        List<AoaTaskUser> taskUsers=taskListDao.findAllTaskUserByTaskId(taskId+"");
        for (AoaTaskUser taskUser : taskUsers) {
            taskListDao.delTask(taskId,taskUser.getTaskReciveUserId());
        }
        System.out.println("已经删除");
        return "forward:/taskmanage?pageNum="+pageNum;
    }
}
